package main

import (
	"flag"
	"gitee.com/chejiangyi/bsfgo/core/utils"
	"io/ioutil"
	"strings"
)

func main() {
	// 定义参数变量
	var (
		pkg              string
		language         string
		outDir           string
		swaggerJsonPath  string
		replaceTypeNames string
	)
	// 绑定参数到变量
	//flag.StringVar(&pkg, "package", "com.free.bsf.demo.example", "包名")
	//flag.StringVar(&language, "language", "java", "开发语言")
	flag.StringVar(&pkg, "package", "demo_example", "包名")
	flag.StringVar(&language, "language", "go", "开发语言")
	flag.StringVar(&outDir, "outDir", "out", "输出目录")
	flag.StringVar(&swaggerJsonPath, "swaggerJsonPath", "http://localhost:8081/swagger/_doc.json", "json文件(http url或文件路径)")
	flag.StringVar(&replaceTypeNames, "replaceTypeNames", "auto_models_,http_models_,base2_", "需要替换部分类型名为空,逗号分隔多个")
	// 解析参数
	flag.Parse()
	utils.LogUtil.Info("package:%v ", pkg)
	utils.LogUtil.Info("language:%v ", language)
	utils.LogUtil.Info("outDir:%v ", outDir)
	utils.LogUtil.Info("swaggerJsonPath:%v ", swaggerJsonPath)
	utils.LogUtil.Info("replaceTypeNames:%v ", replaceTypeNames)

	utils.LogUtil.Info("代码开始生成...")
	var bs []byte
	if strings.HasPrefix(swaggerJsonPath, "http:") || strings.HasPrefix(swaggerJsonPath, "https:") {
		rs, err := utils.HttpUtil.GetDefaultClient().R().Get(swaggerJsonPath)
		if err != nil {
			panic(err)
		}
		bs = rs.Body()
	} else {
		bs2, err := ioutil.ReadFile(swaggerJsonPath)
		if err != nil {
			panic(err)
		}
		bs = bs2
	}

	parser := &SwaggerParser{}
	api := parser.Parser(bs)
	parser.Template(pkg, language, outDir, api, strings.Split(replaceTypeNames, ","))
	utils.LogUtil.Info("代码生成完毕！！！")
}
